<%-- 
    Document   : ViewFile
    Created on : Oct 26, 2014, 9:18:11 AM
    Author     : user
--%>

<%@page import="newpackage.SendEmailKey"%>
<%@page import="java.io.FileNotFoundException"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="newpackage.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript">
            function myFunction(name) {
                
            
    var key = prompt("Please enter Secret Key", "Please enter Secret Key");
    if (key != null) {
        viewFile(name,key)
       
    }
}
            function viewFile(name,key)
            {
                document.getElementById("key").value=key;
                document.getElementById("file_name").value=name;
                document.forms["view_file"].submit();
               
            }
            function alertmsg()
            {
                alert("File Not Found");
            }
            
             function submitForm()
            {
                alert('hai');
               var selectBox = document.getElementById("file_name");
               var selectedValue = selectBox.options[selectBox.selectedIndex].value;
               window.location.assign('SaveFile.jsp?file_name='+selectedValue);
            }
        </script>
    </head>
    <body background="image.jpg">
        <h1> ENSURING PRIVACY AND DATA FRESHNESS OF SHARED DATA IN CLOUD </h1>
        <center>
        <form id="view_file" name="view_file" >
        <%!ArrayList<String> getFileNames(String user)throws Exception
         {
         ArrayList<String>  names=new ArrayList<String>();
           Connection con=ConnectionFactory.getConnection();
           Statement select=con.createStatement();
           String Query="SELECT file_details.filename FROM file_details INNER JOIN shared ON shared.id_fk=file_details.id WHERE shared.user_name='"+user+"' ";
           ResultSet rs=select.executeQuery(Query);
           int i=0;
           while(rs.next())
           {
               String s=rs.getString(1);
               names.add(s);
           }
           return names;
        }
        %>
        <%!void sendMail(String fn, String un)throws Exception
                               {
             Connection con=ConnectionFactory.getConnection();
           Statement select=con.createStatement();
           String Query="SELECT file_details.secret_key,shared.user_name FROM file_details inner join shared on shared.id_fk=file_details.id where file_details.filename='"+fn+"' and user_name='"+un+"'";
           ResultSet rs=select.executeQuery(Query);
           String s1="";
           String s2="";
         while(rs.next())
                           {
         //     SendEmailKey.sendMail(rs.getString(2),rs.getString(1));
             s1=rs.getString(2);
             s2=rs.getString(1);
              
          }
           SendEmailKey.sendMail(s1,s2);
        }
        
%>
        
        <%
     String user_name=(String)session.getAttribute("user_name");
     ArrayList<String> files=getFileNames(user_name);
        %>
        <p>
            Select file Name:
        </p>
        
        <select name="file_name" onchange="myFunction(this.value)" id="file_name">
     
        <option >select file</option>
          <%
          
       if(request.getParameter("file_name")!=null)
       {           sendMail(request.getParameter("file_name"),session.getAttribute("user_name").toString());
          %>
          <option value="<%=request.getParameter("file_name")%>" selected="" ><%=request.getParameter("file_name").substring(request.getParameter("file_name").lastIndexOf('/')+1) %></option>
      <%
      } 
       for(String name:files)
        {
           if(request.getParameter("file_name")!=null)
                             {
       if(request.getParameter("file_name").equals(name))
        continue;     
           }               
         %>
    <option value="<%=name%>"><%=name.substring(name.lastIndexOf('/')+1)%></option>
        <%
        }
        %>
        </select>
        <input type="hidden" name="key" id="key"/>
        
        <textarea style="width: 1091px; height: 339px;" name="file_content">
          <%
          try
          {
              
          if((request.getParameter("file_name")!=null)&&request.getParameter("key")!=null)
          {
              String key=request.getParameter("key");
              String file_name=request.getParameter("file_name");
              String query="SELECT * from file_details where filename='"+file_name+"' and secret_key='"+key+"'";
              Connection con=ConnectionFactory.getConnection();
              Statement st=con.createStatement();
           ResultSet rs1=st.executeQuery(query);
           if(rs1.next())
                             {
              String fileName=request.getParameter("file_name");
       BufferedReader br=new BufferedReader(new FileReader(fileName)) ;  
       String s;
while((s=br.readLine())!=null)          
       {
    out.println(s);
       }
          }
           else
           {
        out.print("Secretkey is wrong");       
           }
        }
          
          }
          catch(FileNotFoundException e)
       {
      out.println("File Not Found");
          }
          %>
        </textarea>
<!--        <input type="button" value="Save" onclick="submitForm()"/>-->
  <input type="submit" formmethod="post" formaction="SaveFile.jsp"  value="SAVE"/>
<a href="Login.jsp">LogOut</a>
        </form>
        </center>
    </body>
</html>
